package com.example.rbacmanage.controller.background;

import com.example.rbacmanage.common.Result;
import com.example.rbacmanage.entity.app.Tag;
import com.example.rbacmanage.service.app.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("/background/tag")
public class TagController {
    @Autowired
    private TagService tagService;

    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('background:tag:list')")
    public Result page(Tag tag){
        return Result.success(tagService.getPage(tag));
    }

    @GetMapping("/alllist")
    public Result alllist(Tag tag){
        return Result.success(tagService.getAll(tag));
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Long id){
        return Result.success(tagService.getById(id));
    }
    @PreAuthorize("hasAnyAuthority('background:tag:add')")
    @PostMapping("/add")
    public Result add(@RequestBody Tag tag){
        tag.setCreateTime(new Date());
        tagService.save(tag);
        return Result.success();
    }
    @PreAuthorize("hasAnyAuthority('background:tag:update')")
    @PutMapping("/update")
    public Result update(@RequestBody Tag tag){
        tagService.update(tag);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('background:tag:remove')")
    @DeleteMapping("/remove")
    public Result remove(@RequestBody Long[] ids){
        tagService.remove(ids);
        return Result.success();
    }
}
